import 'package:flutter/material.dart';
import 'package:helloworld/res/debtData.dart';
import 'package:helloworld/tabs.dart';
import 'package:dio/dio.dart';
import 'dart:ui';
import 'package:loading/loading.dart';
import 'package:loading/indicator/ball_pulse_indicator.dart';
class DebtDetail extends StatefulWidget{
  final arguments;
  DebtDetail({this.arguments});
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _DebtDetail(arguments:this.arguments);
  }
}

class _DebtDetail extends State<DebtDetail>{
  var _name = TextEditingController();
  var _nameData;
  var _note = TextEditingController();
  var _noteData;
  var _debt = TextEditingController();
  var _debtData;
  Brightness brightnessValue ;
  final arguments;
  _DebtDetail({this.arguments});
  _delete(){
    showDialog(context: context,builder: (context){
      return AlertDialog(
        content: Text('您确定要删除吗?',style: TextStyle(fontSize: 20),),
        actions: <Widget>[
          FlatButton(
            child: Text('取消',style: TextStyle(fontSize: 20),),
            onPressed: (){
              Navigator.pop(context);
            },
          ),
          FlatButton(
            child: Text('确定',style: TextStyle(fontSize: 20),),
            onPressed: (){
              Navigator.pop(context);
              Dio().post("http://123.207.114.127:1314/debt/deleteDebt",queryParameters: {
                'debtid':debtData[this.arguments['id']]['debtid']
              });
              Future.delayed(Duration(milliseconds: 50), (){
                Navigator.of(context).pop();
                Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new Tabs( index:3)), (route) =>route==null);
              });
            },
          ),

        ],
      );
    });

  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _name.text = debtData[this.arguments['id']]['debtname'];
    _nameData = debtData[this.arguments['id']]['debtname'];
    _note.text = debtData[this.arguments['id']]['debtnote'];
    _noteData = debtData[this.arguments['id']]['debtnote'];
    _debt.text = (-debtData[this.arguments['id']]['debt']).toStringAsFixed(2);
    _debtData = (-debtData[this.arguments['id']]['debt']).toStringAsFixed(2);
    brightnessValue = window.platformBrightness;
  }
  updateDebt()async{
    Dio().post("http://123.207.114.127:1314/debt/updateDebt",queryParameters: {
      'debtid':debtData[this.arguments['id']]['debtid'],
      'debtname':_nameData,
      'debtnote':_noteData,
      'debt': -double.parse(_debtData)
    });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text('编辑负债'),
        actions: <Widget>[
          InkWell(
            child: Padding(
              child:Text('保存',style: TextStyle(color: Colors.white,fontSize: 20),),
              padding: EdgeInsets.all(16),
            ),
            onTap: (){
              updateDebt();
              Future.delayed(Duration(milliseconds: 50), (){
                Navigator.of(context).pop();
                Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new Tabs( index:3)), (route) =>route==null);
              });
            },
          )
        ],
      ),
      body:  Padding(
        padding: EdgeInsets.all(20),
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              TextField(
                keyboardAppearance: brightnessValue,
                controller: _name,
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: '名称'
                ),
                onChanged: (value){
                  setState(() {
                    _nameData = value;
                  });
                },
              ),
              // 10 iphone11
              SizedBox(height: 10,),
              TextField(
                keyboardAppearance: brightnessValue,
                controller: _note,
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: '备注'
                ),
                onChanged: (value){
                  setState(() {
                    _noteData = value;
                  });
                },
              ),
              // 10 iphone11
              SizedBox(height: 10,),
              TextField(
                keyboardAppearance: brightnessValue,
                keyboardType: TextInputType.numberWithOptions(decimal: true),
                controller: _debt,
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: '金额'
                ),
                onChanged: (value){
                  setState(() {
                    _debtData = value;
                  });
                },
              ),
              SizedBox(height: 50,),
              Container(
                width: 150,
                height: 49,
                child: RaisedButton(
                  color: Colors.lightBlueAccent,
                  textColor: Colors.white,
                  child: Text('删除',style: TextStyle(fontSize: 20),),
                  onPressed: _delete,
                ),
              )
            ],
          ),
          scrollDirection: Axis.vertical,
        ),
      ),
    );
  }
}